package cn.threecubic.springcloud.service.impl;

import cn.threecubic.springcloud.dao.OrderDao;
import cn.threecubic.springcloud.domain.Order;
import cn.threecubic.springcloud.service.AccountService;
import cn.threecubic.springcloud.service.OrderService;
import cn.threecubic.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Terminator
 * @date 2021/12/12 21:56
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StorageService storageService;


    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----创建订单");
        orderDao.create(order);
        log.info("----调用库存服务开始");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----调用库存服务结束");

        log.info("----调用账户服务开始");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----调用账户服务结束");

        log.info("----修改订单状态开始");
        orderDao.update(order.getUserId(), 1);
        log.info("----修改订单状态结束");

        log.info("下单结束。。。。。");
    }
}
